package com.openbravo.pos.panels;

import javax.swing.JPanel;
import javax.swing.JLabel;

import cn.ekuma.epos.datalogic.I_DataLogicSales;

import com.openbravo.bean.crm.CustomerStoreValueCard;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.editor.JEditorKeys;
import com.openbravo.editor.JEditorPassword;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.util.RoundUtils;

import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;

public class JStoreValueCardFinder extends JPanel{
	private JTextField txtCurLimit;
	private JTextField txtBackLimit;
	private JTextField txtValidityDate;
	private JTextField txtState;
	private JEditorPassword txtCardNum;
	private CustomerStoreValueCard card;
	private I_DataLogicSales dSales;

	public JStoreValueCardFinder(I_DataLogicSales dSales,JEditorKeys editorKeys){
		setPreferredSize(new Dimension(360, 180));
		this.dSales=dSales;
		initComponents();
		if(editorKeys!=null)
		   txtCardNum.addEditorKeys(editorKeys);
		
	}
	private void initComponents() {
		setLayout(null);
		
		JLabel label = new JLabel(AppLocal.getIntString("label.CustomerStoreValueCard.cardNum"));
		label.setBounds(20, 20, 100, 15);
		add(label);
		
		txtCardNum = new JEditorPassword();
		txtCardNum.setBounds(120, 10, 177, 25);
		add(txtCardNum);
		
		JLabel label_1 = new JLabel();
		label_1.setText(AppLocal.getIntString("label.CustomerStoreValueCard.curLimit"));
		label_1.setBounds(20, 45, 100, 15);
		add(label_1);
		
		txtCurLimit = new JTextField();
		txtCurLimit.setHorizontalAlignment(SwingConstants.RIGHT);
		txtCurLimit.setEditable(false);
		txtCurLimit.setBounds(120, 45, 130, 19);
		add(txtCurLimit);
		
		JLabel label_2 = new JLabel();
		label_2.setText(AppLocal.getIntString("label.CustomerStoreValueCard.backLimit"));
		label_2.setBounds(20, 75, 100, 15);
		add(label_2);
		
		txtBackLimit = new JTextField();
		txtBackLimit.setHorizontalAlignment(SwingConstants.RIGHT);
		txtBackLimit.setEditable(false);
		txtBackLimit.setBounds(120, 75, 130, 19);
		add(txtBackLimit);
		
		JLabel label_3 = new JLabel();
		label_3.setText(AppLocal.getIntString("label.CustomerStoreValueCard.validityDate"));
		label_3.setBounds(20, 100, 100, 15);
		add(label_3);
		
		txtValidityDate = new JTextField();
		txtValidityDate.setHorizontalAlignment(SwingConstants.CENTER);
		txtValidityDate.setEditable(false);
		txtValidityDate.setBounds(120, 100, 130, 19);
		add(txtValidityDate);
		
		JLabel lblNewLabel = new JLabel(AppLocal.getIntString("label.CustomerStoreValueCard.state"));
		lblNewLabel.setBounds(20, 125, 100, 15);
		add(lblNewLabel);
		
		txtState = new JTextField();
		txtState.setEditable(false);
		txtState.setBounds(120, 122, 130, 21);
		add(txtState);
		txtState.setColumns(10);
		
		JButton btnNewButton = new JButton("");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				findStoreValueCardByCardNum();
			}
		});
		btnNewButton.setIcon(new ImageIcon(JStoreValueCardFinder.class.getResource("/com/openbravo/images/apply.png")));
		btnNewButton.setBounds(301, 10, 38, 23);
		add(btnNewButton);
	}
	public void findStoreValueCardByCardNum() {
		try {
			card=dSales.findStoreValueCardByNum(txtCardNum.getText());
		} catch (BasicException e) {
			new MessageInf(e).show(this);
		}
		if(card==null){
			txtCurLimit.setText(null);
	        txtValidityDate.setText(null);        
	        txtBackLimit.setText(null);
	        txtState.setText(null);
		   MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.NoFoundStoreCard"));
           msg.show(this);
		}else{
			txtCurLimit.setText(Formats.CURRENCY.formatValue(RoundUtils.getValue(card.getCurLimit())));
			txtValidityDate.setText(Formats.DATE.formatValue(card.getValidityDate()));        
			txtBackLimit.setText(Formats.CURRENCY.formatValue(RoundUtils.getValue(card.getBackLimit()))); 
			txtState.setText(getState(card.getState()));
		}
	}
	public CustomerStoreValueCard getCard() {
		return card;
	}
	public void setCard(CustomerStoreValueCard card) {
		this.card = card;
	}
	
	 private static  final String[] states=new String[]{
	    	AppLocal.getIntString("label.CustomerStoreValueCard.state.Unknown"),
	    	AppLocal.getIntString("label.CustomerStoreValueCard.state.UseAble"),
	    	AppLocal.getIntString("label.CustomerStoreValueCard.state.Back"),
	    	AppLocal.getIntString("label.CustomerStoreValueCard.state.Invalid"),
	    	AppLocal.getIntString("label.CustomerStoreValueCard.state.Loss")
	    };

	public static String getState(int state){
		 if(state<states.length)
	    	    return  states[state];
	    	 return String.valueOf(state);
	}
	
	public void activate(){
		txtCardNum.activate();
	}
	
	public void reset(){
		card=null;
		txtCardNum.reset();
		txtCurLimit.setText(null);
        txtValidityDate.setText(null);        
        txtBackLimit.setText(null);
        txtState.setText(null);
	}
}
